const express = require("express");
const data = require("./data/index");
const server = express();
server.get("/", (req, res) => {
  // console.log(data);
  let str = `<ul>`;
  data.forEach((item) => {
    str += `<li><a href='/detail/${item.id}'>${item.newsTitle}</a></li>`;
  });
  str += `</ul>`;
  res.send(str);
});

server.get("/detail/:id", (req, res) => {
  let { id } = req.params;
  let { newsTitle, newsContent } = data.find((v) => {
    return v.id === id;
  });
  let str = `<h1>${newsTitle}</h1><p>${newsContent}</p>`;
  res.send(str);
});

server.listen(80, () => {
  console.log("success");
});
